import md5 from "../static/js_sdk/md5.js"
module.exports = (params) => {
	let url = params.url;	
	let domain = 'https://api.gubidiguo.com/';
	let method = params.method;	
	let header = params.header || {};	
	let data = params.data || {};
	
	let debug = params.debug||false
	//	请求方式 GET POST
	if	(method) {
		method = method.toUpperCase();	//	小写转大写
		if (method == "POST") {
			header = {"content-type":"application/x-www-form-urlencoded"}
		}
	}
	//	发起请求 加载动画
	if (!params.hideLoading){
		uni.showLoading({
			title:"加载中",
			mask:true
		})
		
	}
	let token = ''
	try {
	    const value = uni.getStorageSync('token')||'';
		console.log('当前token----------',value)
	    if (value) {
			token = value
	    }else{
			
		}
	} catch {
	    // error
	
	}
	let sign = ''
	let newData = JSON.parse(JSON.stringify(data));
	newData.key= 'MINGYANGB7D0C9D279892857DE5490AB856772FE' //口令
	newData.domain=domain//域名
	newData.pathinfo=url//接口地址
	newData.timestamp=new Date().getTime()//当前时间戳
	sign = objKeySort(newData)
	function objKeySort(obj) {//排序的函数
	    var newkey = Object.keys(obj).sort();
	    var newObj = '';
	    for (var i = 0; i < newkey.length; i++) {
	        // newObj[newkey[i]] = obj[newkey[i]];
	        newObj =newObj+`&${[newkey[i]]}=${obj[newkey[i]]}`;
	    }
	    return newObj.substring(1);
	}
	console.log('当前-------------------------',sign)
	header = {"content-type":"application/json;charset=UTF-8","token":token,"timestamp":newData.timestamp,"debug":debug,"sign":md5(sign)}
	//	发起网络请求
	uni.request({
		// url:'/api/'+url,
		url:domain+url,
		method:method || "GET",
		header:header,
		data:data,
		dataType:"json",
		sslVerify:false,	//	是否验证ssl证书
		success: res => {
			// console.log('服务端发送结果------------',res)
			if	(res.statusCode && res.data.code == 400){
				//	api错误
				// uni.showModal({
				// 	content:res.data.msg
				// })
				console.log('服务端发送结果------------',res)
				uni.stopPullDownRefresh();
				
				if(res.statusCode==500){
					uni.showToast({
					    title: '500-服务器内部错误',
					    duration: 2000,
						icon:'none'
					});
					return;
				}else if(res.statusCode==404){
					uni.showToast({
					    title: '404-所请求的页面不存在',
					    duration: 2000,
						icon:'none'
					});
					return;
				}else if(res.data.code == 400 && res.data.msg=='Token验证失败'){
					uni.showToast({
					    title: '登录过期请重新登录',
					    duration: 2000,
						icon:'none'
					});
					try {
					    uni.removeStorageSync('token');
					} catch (e) {
					    // error
					}
					uni.redirectTo({
					    url: '/pages/Login/index'
					});
					return;
				}else if(res.data.code == 400 && res.data.msg=='缺少Token参数'){
					uni.showToast({
					    title: '登录过期请重新登录',
					    duration: 2000,
						icon:'none'
					});
					
					uni.redirectTo({
					    url: '/pages/Login/index'
					});
					return;
				}
				uni.showToast({
				    title: res.data.msg || '未知错误'+res.statusCode,
				    duration: 2000,
					icon:'none'
				});
				return;
			}
			if(res.data.code == 403){
				try {
				    uni.removeStorageSync('token');
				} catch (e) {
				    // error
				}
				uni.redirectTo({
				    url: '/pages/Login/index'
				});
				return
			}
			if(res.data.code == 500){
				uni.showToast({
				    title: res.data.msg,
				    duration: 2000,
					icon:'none'
				});
				return;
			}
			if(res.data.code == 201){
				if(res.data.data.token){
					token = res.data.data.token
				}
				let timestamp=new Date().getTime()//当前时间戳
				let sign = objKeySort({key:'MINGYANGB7D0C9D279892857DE5490AB856772FE',domain:domain,pathinfo:'syncData',timestamp:timestamp,is_sync:1})
				console.log('同步数据-----------------sing',sign)
				uni.request({
				    url:domain+'syncData',
				    method:"POST",
				    header:{"content-type":"application/json;charset=UTF-8","token":token,"timestamp":timestamp,"debug":false,"sign":md5(sign)},
				    data:{
						is_sync:true
					},
				    dataType:"json",
				    sslVerify:false,
				    success: res => {
				    	console.log('同步数据------------',res)
						try {
						    uni.setStorageSync('userInfo', res.data.data);
						} catch (e) {
						    // error
						}
				    },
				});
			}
			typeof params.success == "function" && params.success(res.data);
		},
		fail: err => {
			if(err.errMsg=='request:fail abort statusCode:-1 timeout'){
				uni.showToast({
				    title: '连接超时,请检查您的网络设置',
				    duration: 2000,
					icon:'none'
				})
				return
			}
			uni.showToast({
			    title: err.msg || '未知错误',
			    duration: 2000,
				icon:'none'
			});
			typeof params.fail == "function" && params.fail(err.data);
		},
		complete: (e) => {
			console.log("---------------------请求完成------------------------",e);
			uni.hideLoading()
			uni.stopPullDownRefresh();
			typeof params.complete == "function" && params.complete(e.data);
			return;
		}
	})
}